
const path = require('path')
const axios = require('axios')
const bodyParser = require('body-parser')

function resolve(fileName){
    return path.join(__dirname,fileName)
}

function banner_format(list){
    return list.map(item => {
        return {
            id: item.id,
            link: `https://y.qq.com/n/yqq/playlist/${item.jump_info.url}.html`,
            pic: item.pic_info.url
        }
    })
}

function disc_format(list){
    return list.map(item => {
        return {
            id: item.dissid,
            avatar: item.imgurl,
            name: item.creator.name,
            desc: item.dissname
        }
    })
}

function discSong_format(list){
   return list.map(song => {
       let info = {
           musicData: {}
       }
       info.musicData.songid = song.id
       info.musicData.songmid = song.mid
       info.musicData.songname = song.name
       info.musicData.albumname = song.album.name
       info.musicData.albummid = song.album.mid
       info.musicData.interval = song.interval
       info.musicData.url = song.url
       info.musicData.singer = song.singer
       info.musicData.pay = song.pay
       info.musicData.pay.payalbumprice = song.pay.price_album

       return info
   })
}


module.exports= {
    chainWebpack: config => {
        config.resolve.alias
            .set('common',resolve('src/common'))
    },
    devServer: {
        before(app){
            app.get('/api/banner',async (req,res)=>{
                const url = 'https://u.y.qq.com/cgi-bin/musics.fcg'
                let result = await axios.get(url,{
                    params: req.query,
                    headers: {
                        origin: 'https://y.qq.com',
                        referer: 'https://y.qq.com/'
                    }
                })
                const content = banner_format(result.data.focus.data.content)
                res.json(content)
            }),
            app.get('/api/disc',async (req,res) => {
                const url = 'https://c.y.qq.com/splcloud/fcgi-bin/fcg_get_diss_by_tag.fcg'
                let result = await axios.get(url,{
                    params: req.query,
                    headers: {
                        origin: 'https://y.qq.com',
                        referer: 'https://y.qq.com/portal/playlist.html'
                    }
                })
                const data = disc_format(result.data.data.list)
                res.json(data)
            }),
            app.get('/api/discsong',async (req,res) => {
                const url = 'https://c.y.qq.com/qzone/fcg-bin/fcg_ucc_getcdinfo_byids_cp.fcg'
                let ret = (await axios.get(url,{
                    params: req.query,
                    headers: {
                        referer: 'https://c.y.qq.com/',
                        host: 'c.y.qq.com'
                    }
                })).data
                if (typeof ret === 'string') {
                    const reg = /^\w+\(({.+})\)$/
                    const matches = ret.match(reg)
                    if (matches) {
                        ret = JSON.parse(matches[1])
                    }
                }
                res.json(discSong_format(ret.cdlist[0].songlist))
            }),
            app.get('/api/lyric',async (req,res) => {
                const url = 'https://c.y.qq.com/lyric/fcgi-bin/fcg_query_lyric_new.fcg'
                let result = (await axios.get(url, {
                    params: req.query,
                    headers: {
                        referer: 'https://c.y.qq.com/',
                        host: 'c.y.qq.com'
                    }
                })).data
                res.json(result.lyric)
            }),
            app.post('/api/getPurlUrl',bodyParser.json(),async (req,res) => {
                const url = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
                let result = await axios.post(url,req.body,{
                    headers: {
                        referer: 'https://y.qq.com/',
                        origin: 'https://y.qq.com',
                        'Content-type': 'application/x-www-form-urlencoded'
                    }
                })
                res.json(result.data)
            }),
            app.get('/api/search',async (req,res) => {
                const url = 'https://c.y.qq.com/soso/fcgi-bin/search_for_qq_cp'
                res.json((await axios.get(url, {
                    params: req.query,
                    headers: {
                        referer: 'https://c.y.qq.com/',
                        host: 'c.y.qq.com'
                    }
                })).data)
            })
        }
    },
    publicPath: './'
}
